package com.ricchieri.manage.bean;

import java.util.HashMap;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.CharterExpenseFilter;
import com.ricchieri.filter.CharterFilter;
import com.ricchieri.lazymodel.CharterExpenseLazyDataModel;
import com.ricchieri.model.Charter;
import com.ricchieri.model.CharterExpense;
import com.ricchieri.reports.ReportFactory;
import com.ricchieri.reports.ReportFileFormat;
import com.ricchieri.reports.ReportTemplate;
import com.ricchieri.service.interf.ICharterExpenseService;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "viewCharterMB")
@ViewScoped
public class ViewCharterManageBean extends GenericManageBean {

	private static final long serialVersionUID = -4431457088573798836L;
	
	private Charter selectedCharter;
	
	private CharterExpense selectedCharterExpsense;
	private CharterExpenseLazyDataModel charterExpenseLazyDataModel;
	
	@ManagedProperty(value = "#{CharterExpenseService}")
	ICharterExpenseService charterExpenseService;

	private CharterFilter charterFilter;
	
	public void init() {
		selectedCharter = (Charter) FacesUtil.getParametersFlash(BusinessSettings.KEY_CHARTER);
		charterFilter = (CharterFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);

		if (!FacesContext.getCurrentInstance().isPostback() && selectedCharter == null) {
			FacesUtil.redirectToNavigationCase(BusinessSettings.SEARCH_CHARTER);
		}
	}
	
	public Charter getSelectedCharter() {
		return selectedCharter;
	}

	public void setSelectedCharter(Charter selectedCharter) {
		this.selectedCharter = selectedCharter;
	}

	public CharterExpenseLazyDataModel getCharterExpenseLazyDataModel() {
		if (charterExpenseLazyDataModel == null && getSelectedCharter() != null) {
			CharterExpenseFilter charterFilter =  new CharterExpenseFilter();
			charterFilter.setCharterId(getSelectedCharter().getId());
			charterExpenseLazyDataModel = new CharterExpenseLazyDataModel(getCharterExpenseService(), charterFilter );
		}
		return charterExpenseLazyDataModel;
	}

	public void setCharterExpenseLazyDataModel(
			CharterExpenseLazyDataModel charterExpenseLazyDataModel) {
		this.charterExpenseLazyDataModel = charterExpenseLazyDataModel;
	}

	public ICharterExpenseService getCharterExpenseService() {
		return charterExpenseService;
	}

	public void setCharterExpenseService(
			ICharterExpenseService charterExpenseService) {
		this.charterExpenseService = charterExpenseService;
	}
	
	public String addCharterExpense() {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER, selectedCharter);
		
		return BusinessSettings.ADD;
	}
	
	public String viewCharterExpense(CharterExpense expense) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER, selectedCharter);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER_EXPENSE, expense);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, charterFilter);
		
		return BusinessSettings.VIEW_DETAILS;
	}
	
	public void showDialog(CharterExpense charter) {
		selectedCharterExpsense = charter;
	}
	
	public void delete() {
		getCharterExpenseLazyDataModel().setRowCount(getCharterExpenseLazyDataModel().getRowCount() -1 );
		getCharterExpenseService().deleteCharterExpense(getSelectedCharterExpsense());
		FacesUtil.showInfoMessage("searchCharterForm", "charterExpenseDeleted");
	}
 
	public CharterExpense getSelectedCharterExpsense() {
		return selectedCharterExpsense;
	}

	public void setSelectedCharterExpsense(CharterExpense selectedCharterExpsense) {
		this.selectedCharterExpsense = selectedCharterExpsense;
	}
	
	public void unselect() {
		selectedCharterExpsense = null;
	}
	
	public void createReport() {
		ReportTemplate report = ReportFactory.getReport(BusinessSettings.CHARTER_REPORT);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(BusinessSettings.KEY_CHARTER, getSelectedCharter());
		report.generateReport(map , ReportFileFormat.PDF);
	}
	
	public String editCharterExpense(CharterExpense expense) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER, expense.getCharter());
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER_EXPENSE, expense);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, charterFilter);
		
		return BusinessSettings.EDIT;
	}
	
	public String doBackToSearchCharter() {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, charterFilter);
		
		return BusinessSettings.SEARCH_CHARTER;
	}
}
